package com.novelot.plugin.allinone.switc;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.wm.StatusBarWidgetProvider;
import com.intellij.psi.JavaDirectoryService;
import com.intellij.psi.PsiDirectory;
import com.novelot.plugin.allinone.setting.AppSettingsState;
import com.novelot.plugin.allinone.util.AppExecutors;
import com.novelot.plugin.allinone.util.IdeaApi;
import com.novelot.plugin.allinone.util.Logger;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

public class SwitchView {

    public static String path = "/Users/momo/Projects/Momo/android_hanisdk";
    private Project mProject;

    private DefaultListModel listModel = new DefaultListModel();
    private JList<SwitchBean> listView;
    private JTextField tfKey;
    private JTextField tfPath;

    public SwitchView(Project project) {
        this.mProject = project;
    }

    public JPanel createSwitchView() {
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        //
        //添加配置条
        JPanel configPanel = new JPanel();
        GridBagConstraints configPanelConstraints = new GridBagConstraints();
        configPanelConstraints.gridx = 1;
        configPanelConstraints.gridy = 1;
        configPanelConstraints.gridwidth = 1;
        configPanelConstraints.gridheight = 1;
        configPanelConstraints.fill = GridBagConstraints.HORIZONTAL;
        panel.add(configPanel, configPanelConstraints);

        configPanel.setLayout(new GridBagLayout());


        //项目路径
        JLabel labelPath = new JLabel("项目路径:");
        GridBagConstraints labelPathConstraints = new GridBagConstraints();
        labelPathConstraints.gridx = 1;
        labelPathConstraints.gridy = 1;
        labelPathConstraints.gridwidth = 1;
        labelPathConstraints.gridheight = 1;
        configPanel.add(labelPath, labelPathConstraints);

        tfPath = new JTextField();
        tfPath.setText(path);
        GridBagConstraints tfPathConstraints = new GridBagConstraints();
        tfPathConstraints.gridx = 2;
        tfPathConstraints.gridy = 1;
        tfPathConstraints.gridwidth = 1;
        tfPathConstraints.gridheight = 1;
        tfPathConstraints.weightx = 1;
        tfPathConstraints.fill = GridBagConstraints.HORIZONTAL;
        configPanel.add(tfPath, tfPathConstraints);

        //key
        JLabel labelKey = new JLabel("KEY:");
        GridBagConstraints constraintsKey = new GridBagConstraints();
        constraintsKey.gridx = 1;
        constraintsKey.gridy = 2;
        constraintsKey.gridwidth = 1;
        constraintsKey.gridheight = 1;
        configPanel.add(labelKey, constraintsKey);

        tfKey = new JTextField();
        GridBagConstraints constraintsTfKey = new GridBagConstraints();
        constraintsTfKey.gridx = 2;
        constraintsTfKey.gridy = 2;
        constraintsTfKey.gridwidth = 1;
        constraintsTfKey.gridheight = 1;
        constraintsTfKey.weightx = 1;
        constraintsTfKey.fill = GridBagConstraints.HORIZONTAL;
        configPanel.add(tfKey, constraintsTfKey);

        JButton btnAdd = new JButton();
        btnAdd.setText("添加");
        btnAdd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String hanisdkDir = AppSettingsState.getInstance().hanisdkDir;
                String momoDir = AppSettingsState.getInstance().momoDir;
                Logger.v("从设置获取的数据:hanisdkDir=" + hanisdkDir + ",momoDir=" + momoDir);
                //
                IdeaApi.test(mProject);
                //
                SwitchView.path = tfPath.getText();
                if (SwitchView.path == null || SwitchView.path.isEmpty()) {
                    SwitchView.path = ".";
                }

                String keyText = tfKey.getText();
                if (keyText != null && !keyText.isEmpty()) {
                    String cmdOn = "";
                    String cmdOff = "";
                    SwitchBean bean = new SwitchBean(keyText, false, cmdOn, cmdOff, path);
                    listModel.addElement(bean);
                    if (listView != null) {
                        listView.updateUI();
                    }
                }
            }
        });
        GridBagConstraints constraintsBtnAdd = new GridBagConstraints();
        constraintsBtnAdd.gridx = 3;
        constraintsBtnAdd.gridy = 2;
        constraintsBtnAdd.gridwidth = 1;
        constraintsBtnAdd.gridheight = 1;
//        constraintsBtnAdd.insets = new Insets(8, 8, 8, 8);
        configPanel.add(btnAdd, constraintsBtnAdd);

        //
        GridBagConstraints tabelConstraints = new GridBagConstraints();
        tabelConstraints.gridx = 1;
        tabelConstraints.gridy = 3;
        tabelConstraints.gridwidth = 1;
        tabelConstraints.gridheight = 1;
        tabelConstraints.weightx = 1;
        tabelConstraints.weighty = 1;
        //tabelConstraints.insets = new Insets(8, 8, 8, 8);
        tabelConstraints.fill = GridBagConstraints.BOTH;


        listView = new JList<>(listModel);
        listView.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        listView.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                SwitchView.path = tfPath.getText();
                if (SwitchView.path == null || SwitchView.path.isEmpty()) {
                    SwitchView.path = ".";
                }
                int index = listView.locationToIndex(e.getPoint());
                SwitchBean bean = (SwitchBean) listModel.getElementAt(index);
                bean.update(bean.offon ? false : true);
                if (listView != null) {
                    listView.updateUI();
                }
            }
        });
        listView.setCellRenderer(new MyCellRenderer(listView, listModel));
        JScrollPane scrollPane = new JScrollPane(listView);
        panel.add(scrollPane, tabelConstraints);

        loadData();

        return panel;
    }

    private void loadData() {
        AppExecutors.getInstance().diskIO().execute(new Runnable() {
            @Override
            public void run() {
                listModel.removeAllElements();
                List<SwitchBean> list = new ArrayList<>();

                String devPath = path + "/settings.gradle";
                String cmdDevOn = "sed -i '' \"s/^gradle.ext.dev = true/gradle.ext.dev = false/g\" " + devPath;
                String cmdDevOff = "sed -i '' \"s/^gradle.ext.dev = false/gradle.ext.dev = true/g\" " + devPath;

                list.add(new SwitchBean("Dev", false, cmdDevOn, cmdDevOff, devPath));

                String pathLint = path + "/hanisdk/build.gradle";
                String cmdOff = "sed -i '' \"s/^    debugImplementation \\'com.immomo.molive.rule:molive-lint-rule:0.0.1\\'/    \\/\\/debugImplementation \\'com.immomo.molive.rule:molive-lint-rule:0.0.1\\'/g\"  " + pathLint;
                String cmdOn = "sed -i '' \"s/^    \\/\\/debugImplementation \\'com.immomo.molive.rule:molive-lint-rule:0.0.1\\'/    debugImplementation \\'com.immomo.molive.rule:molive-lint-rule:0.0.1\\'/g\"  " + pathLint;
                list.add(new SwitchBean("Lint", false, cmdOn, cmdOff, pathLint));

                listModel.addAll(list);
                if (listView != null) {
                    listView.updateUI();
                }
            }
        });
    }
}
